package com.example.hxds.bff.driver.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.hxds.bff.driver.controller.form.AcceptNewOrderForm;
import com.example.hxds.bff.driver.controller.form.SearchCustomerInfoInOrderForm;
import com.example.hxds.bff.driver.controller.form.SearchDriverCurrentOrderForm;
import com.example.hxds.bff.driver.controller.form.SearchDriverExecuteOrderForm;
import com.example.hxds.bff.driver.feign.CstServiceApi;
import com.example.hxds.bff.driver.feign.MapServiceApi;
import com.example.hxds.bff.driver.feign.OrderServiceApi;
import com.example.hxds.bff.driver.service.OrderService;
import com.example.hxds.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderServiceApi orderServiceApi;
    @Autowired
    private CstServiceApi cstServiceApi;

    @Override
    public String acceptNewOrder(AcceptNewOrderForm form) {
        R r = orderServiceApi.acceptNewOrder(form);
        return MapUtil.getStr(r,"result");
    }

    @Override
    public HashMap searchDriverExecuteOrder(SearchDriverExecuteOrderForm form) {
        //查询订单信息
        R r = orderServiceApi.searchDriverExecuteOrder(form);
        HashMap orderMap = (HashMap) r.get("result");

        //查询代驾客户信息
        long customerId = MapUtil.getLong(orderMap, "customerId");
        SearchCustomerInfoInOrderForm infoInOrderForm = new SearchCustomerInfoInOrderForm();
        infoInOrderForm.setCustomerId(customerId);
        r = cstServiceApi.searchCustomerInfoInOrder(infoInOrderForm);
        HashMap cstMap = (HashMap) r.get("result");

        HashMap map = new HashMap();
        map.putAll(orderMap);
        map.putAll(cstMap);
        return map;
    }

    @Override
    public HashMap searchDriverCurrentOrder(SearchDriverCurrentOrderForm form) {
        R r = orderServiceApi.searchDriverCurrentOrder(form);
        HashMap orderMap = (HashMap)  r.get("result");
        if (!orderMap.isEmpty()){
            HashMap map=new HashMap();
            long customerId = MapUtil.getLong(orderMap, "customerId");
            SearchCustomerInfoInOrderForm form1=new SearchCustomerInfoInOrderForm();
            form1.setCustomerId(customerId);
            R r1 = cstServiceApi.searchCustomerInfoInOrder(form1);
            HashMap cstMap = (HashMap) r1.get("result");

            map.putAll(orderMap);
            map.putAll(cstMap);
            return map;
        }
        return null;
    }
}
